package com.bilin.coin.activity;

import android.annotation.SuppressLint;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.WebUpDateUrlBackMsgBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;


/**
 * Created by LG on 2017/3/18.
 */
@Route(path = "/common/WebLoadingUpdateActivity")
public class WebLoadingUpdateActivity extends BaseAppCompatActivity {


    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    @BindView(R.id.wv_details)
    WebView wvDetails;


    //String strUrl="https://www.pgyer.com/Q6LN";
    @Autowired
    String strUrl;

    @Autowired
    String mMianAndSet;

    @Override
    protected void initView() {
        setTitle(getString(R.string.version_update_title));
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initData() {

        //wvDetails.setVisibility(View.GONE);
        //url= GdproApplication.strH5Url+"";
        wvDetails.getSettings().setLoadWithOverviewMode(true);
        wvDetails.getSettings().setJavaScriptEnabled(true);
        wvDetails.getSettings().setBlockNetworkImage(false);//解决图片不显示
        //wvIntegral.getSettings().setBuiltInZoomControls(true);
        wvDetails.getSettings().setLoadsImagesAutomatically(true);
        wvDetails.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wvDetails.getSettings().setUseWideViewPort(true);
        //wvDetails.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        if (strUrl.indexOf(".apk") != -1) {
            if (mMianAndSet.equals("mian")) {
                EventBus.getDefault().post(new WebUpDateUrlBackMsgBean(strUrl, true));

            } else {
                EventBus.getDefault().post(new WebUpDateUrlBackMsgBean(strUrl, false));
            }

            finish();
        }
        wvDetails.loadUrl(strUrl);

        wvDetails.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.indexOf(".apk") != -1) {
                    if (mMianAndSet.equals("main")) {
                        EventBus.getDefault().post(new WebUpDateUrlBackMsgBean(url, true));

                    } else {
                        EventBus.getDefault().post(new WebUpDateUrlBackMsgBean(url, false));
                    }
                    //LogUtlis.e("下载地址--"+url);
                    finish();
                }
                wvDetails.loadUrl(url);

                return true;
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }
        });
        wvDetails.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    pbLoading.setProgress(newProgress);
                    pbLoading.setVisibility(View.GONE);
                    //wvDetails.setVisibility(View.VISIBLE);
                } else {
                    pbLoading.setProgress(newProgress);
                    pbLoading.setVisibility(View.VISIBLE);
                    //wvDetails.setVisibility(View.GONE);
                }
            }
        });


    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_webh5_loading;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvDetails.canGoBack()) {
            wvDetails.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wvDetails.clearCache(true);
    }
}
